function newimg = randimg(img, p)

[m,n] = size(img);
newimg = zeros(m,n);
for j=1:n
    for i=1:m
        a = rand;
        if(a < p)
            newimg(i,j) = 1 - img(i,j);
        else
            newimg(i,j) = img(i,j);
        end
    end
end
